require 'location'
require 'message'
#load 'medsim.rb'
#MedSim.run

B_a = 0.5
B_b = 0.5
B_c = 0.5
B_d_1 = 0.5
B_d_2 = 0.5

class MedSim
  
  attr_reader :locations
  @@locations = {'clinic' => Clinic.new('clinic'),
                      'dr_1' => Doctor.new('Dr. 1'),
                      'dr_2' => Doctor.new('Dr. 2'),
                      'hrc' => HealthRecordCenter.new('HRC'),
                      'lab' => Lab.new('Lab'),
                      'pharmacy' => Pharmacy.new('Pharmacy')}
  def initialize
  end
  
  def self.run
    refresh
    puts @@locations.keys.join(",\t") + "\r\n" 
    1.upto(600) do |p|
      # 1 patient arrives every minute
      deliver('clinic',NewPatient.new(p/60)) if p % 60 == 0
      @@locations.values.each {|l| l.check_future_messages}
    
    puts queue_sizes + "\r\n" if p % 60 == 0
      @@locations.values.each {|l| l.process_next}
    end
    puts dump + "---------------------\r\n"
  end
  
  def self.send(name, message, time=0)
    raise "no place called #{name}" if @@locations[name].nil?
    @@locations[name].future_message(message,time)
  end
  
  def self.deliver(name, message)
    @@locations[name].receive(message)
  end
  
  def self.refresh
    @@locations.values.each {|l| l.refresh}
  end
  
  def self.queue_sizes
    @@locations.values.map{|l| l.queue_size}.join(",\t")
  end
  
  def self.dump
    @@locations.values.join
  end
end